{extend name="base"/}
{block name="resources"}
<style>
    .js-pid a{
        margin-left: 20px;
    }
    .ns-form {
        margin-top: 0;
    }
</style>
{/block}
{block name="main"}
<form class="layui-form ns-form">
    <input name="id" type="hidden" class="layui-hide" value="{$data.id}">
    <div class="layui-form-item">
        <label class="layui-form-label mid"><span class="required">*</span>商品品牌：</label>
        <div class="layui-input-block">
            <input name="label_name" type="text" placeholder="请输入商品品牌名称" lay-verify="required" class="layui-input ns-len-mid" value="{$data.label_name|default=''}">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label mid">品牌图标：</label>
        <div class="layui-input-block">
            <div class="upload-img-block">
                <div class="upload-img-box" id="imgUpload">
                    {notempty name="$data.image"}
                    <img src="{:img($data.image)}"/>
                    {else/}
                    <div class="ns-upload-default">
                        <img src="SHOP_IMG/upload_img.png" />
                        <p>点击上传</p>
                    </div>
                    {/notempty}
                </div>
            </div>
        </div>
        <div class="ns-word-aux mid">
            <p>建议尺寸：100px * 100px。格式：jpg、png、jpeg。</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label mid">描述：</label>
        <div class="layui-input-block">
            <textarea name="desc" class="layui-textarea ns-len-mid">{$data.desc|default=''}</textarea>
        </div>
    </div>

    <div class="ns-form-row mid">
        <button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="closeAttrLayer()">返回</button>
    </div>
</form>
{/block}
{block name="script"}
<script>
    var form, add_attr_index = -1;
    var file = "";
    layui.use(['form', 'upload', 'laytpl'], function() {
        var repeat_flag = false; //防重复标识
        upload = layui.upload;
        form = layui.form;
        form.render();

        //普通图片上传-新增
        upload.render({
            elem: '#imgUpload'
            , url: ns.url("shop/upload/image")
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#imgUpload').html("<img src=" + result + " >"); //图片链接（base64）
                });
            }
            , done: function (res) {
                if (res.code >= 0) {
                    file = res.data.pic_path;
                }
                return layer.msg(res.message);
            }
        });

        form.on('submit(save)', function(data) {

            if (repeat_flag) return false;
            repeat_flag = true;

            data.field.image = file;

            $.ajax({
                url: '{:addon_url("shop/goodslabel/edit")}',
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(data) {
                    layer.msg(data.message);
                    if (data.code == 0) {
                        location.href = ns.url("shop/goodslabel/lists")
                    }
                    repeat_flag = false;
                }
            });
            return false;
        });
    });

</script>
{/block}